package com.itheima.web.controller;


import com.github.pagehelper.PageInfo;
import com.itheima.domain.Role;
import com.itheima.domain.User;
import com.itheima.factory.BeanFactory;
import com.itheima.service.UserService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/userServlet")
public class UserServlet extends BaseServlet {
    /**
     * 查询全部user信息的
     *
     * @param req
     * @param resp
     */
    public void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageSize = req.getParameter("pageSize");
        //System.out.println(pageSize);
        String currentPage = req.getParameter("currentPage");
        UserService service = (UserService) BeanFactory.getBean("userService");
        PageInfo<User> user = service.findByPage(pageSize, currentPage);
        //请求转求
        req.setAttribute("page", user);
        String method = "findByPage";
        req.setAttribute("methodName", method);
        req.getRequestDispatcher("/pages/system/user-list.jsp").forward(req, resp);
    }

    /**
     * 添加用户的方法
     *
     * @param req
     * @param resp
     */
    public void addUser(HttpServletRequest req, HttpServletResponse resp) {
        Map<String, String[]> map = req.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user, map);
            UserService service = (UserService) BeanFactory.getBean("userService");
            service.addUser(user);
            resp.sendRedirect(req.getContextPath() + "/userServlet?method=findByPage");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除用户的方法
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("ids");
        System.out.println(id);
        String[] ids = id.split(",");
        UserService service = (UserService) BeanFactory.getBean("userService");
        service.deleteUser(ids);
        resp.sendRedirect(req.getContextPath() + "/userServlet?method=findByPage");
    }

    /**
     * 模糊查询的方法(搜索)
     *
     * @param req
     * @param resp
     */
    public void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");
        username = new String(username.getBytes("iso-8859-1"), "utf-8");
        //System.out.println(username);
        String pageSize = req.getParameter("pageSize");
        String currentPage = req.getParameter("currentPage");
        //System.out.println(username);
        UserService service = (UserService) BeanFactory.getBean("userService");
        PageInfo<User> pageInfo = service.search(username, pageSize, currentPage);
        String method = "search";
        req.setAttribute("username", username);
        req.setAttribute("methodName", method);
        req.setAttribute("page", pageInfo);
        req.getRequestDispatcher("/pages/system/user-list.jsp").forward(req, resp);
    }

    /**
     * 数据回显的方法
     *
     * @param req
     * @param resp
     */
    public void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        UserService service = (UserService) BeanFactory.getBean("userService");
        User user = service.findById(id);
        List<Role> list = service.findRoleByUserId(id);
        user.setRoles(list);
        System.out.println(user);
        //将user 对象放置在请求域中,然后有浏览器端获取
        int status = user.getStatus();
        if (status == 1) {
            user.setStatusStr("开启");
        } else {
            user.setStatusStr("关闭");
        }
        req.setAttribute("user", user);
        req.getRequestDispatcher("/pages/system/user-details.jsp").forward(req, resp);
    }

    public void updateStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String pageSizeStr = req.getParameter("pageSize");
        String currentPageStr = req.getParameter("currentPage");
        System.out.println(pageSizeStr);
        UserService service = (UserService) BeanFactory.getBean("userService");
        User user = service.findById(id);
        int status = user.getStatus();
        int updatestatus;
        if (status == 1) {
            updatestatus = 0;
        } else {
            updatestatus = 1;
        }
        service.updateStatus(id, updatestatus);
        resp.sendRedirect(req.getContextPath() + "/userServlet?method=findByPage&pageSize=" + pageSizeStr + "&currentPage" + currentPageStr);
    }

    //查询所有的角色,将数据回显到/pages/system/user-role-add.jsp界面
    public void userRoleList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService userService = (UserService) BeanFactory.getBean("userService");
        String id = req.getParameter("id");
        req.setAttribute("uid", id);
        //获取所有的角色列表
        List<Role> all = userService.findAllByUserId(id);
        req.setAttribute("role", all);
        req.getRequestDispatcher("/pages/system/user-role-add.jsp").forward(req, resp);
    }

    public void updateRole(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String userId = req.getParameter("userId");
        String[] ids = req.getParameterValues("ids");
        UserService userService = (UserService) BeanFactory.getBean("userService");
        userService.updateRole(userId, ids);
        //重定向到list界面
        resp.sendRedirect(req.getContextPath() + "/userServlet?method=findByPage");
    }

}
